package com.rui.service.impl;

import com.rui.domain.Permission;
import com.rui.domain.User;
import com.rui.domain.UserExample;
import com.rui.mapper.MyCustomMapper;
import com.rui.mapper.UserMapper;
import com.rui.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MyCustomMapper myCustomMapper;

    @Override
    public User findUserByUsername(String username) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(example);
        if (users!=null&&users.size()>0) {
            return users.get(0);
        }
        return null;
    }

    @Override
    public List<Permission> findPermissionByRoleId(int roleId) {
        List<Permission> permissions = myCustomMapper.findPermissionByRoleId(roleId);
        return permissions;
    }

    @Override
    public boolean updatePassword(User user) {
        int row = userMapper.updateByPrimaryKeySelective(user);
        if (row > 0) {
            return true;
        }
        return false;
    }
}
